//
//  SQNavigationController.swift
//  SwiftWeibo
//
//  Created by suqianghotel on 16/11/14.
//  Copyright © 2016年 suqianghotel. All rights reserved.
//

import UIKit

class SQNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.interactivePopGestureRecognizer?.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        var backText = "返回"
        
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            if childViewControllers.count == 1 {
                backText = (childViewControllers[0] as UIViewController).title ?? backText
            }
            
            let spacer = UIBarButtonItem.space
            
            let back = UIBarButtonItem(title :backText,
                                       imageName:"navigationbar_back_withtext",
                                       highlighted: "navigationbar_back_withtext_highlighted",
                                       target:self,
                                       action:#selector(SQNavigationController.back))
            
            
            viewController.navigationItem.leftBarButtonItems = [spacer, back]
            
        }
        
        super.pushViewController(viewController, animated: animated);
        
        
    }
    
    
    @objc private func back()
    {
        self.popViewController(animated: true)
    }
    
    
    
    

}
